package com.app.studyroom.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.app.studyroom.R;
import com.app.studyroom.api.model.message.SeatRateItem;
import com.github.lzyzsd.circleprogress.DonutProgress;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SeatRateAdapter extends RecyclerView.Adapter<SeatRateAdapter.SeatRateViewHolder> {

    private final List<SeatRateItem> seatRates;

    public SeatRateAdapter(List<SeatRateItem> seatRates) {
        this.seatRates = seatRates;
    }

    @NonNull
    @Override
    public SeatRateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seat_rate, parent, false);
        return new SeatRateViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SeatRateViewHolder holder, int position) {
        SeatRateItem item = seatRates.get(position);
        holder.tvRoomName.setText(item.qyName);
        try {
            float percent = Float.parseFloat(item.bookingRate.replace("%", ""));
            holder.progressView.setDonut_progress(String.valueOf((int) percent));
            holder.progressView.setText(item.bookingRate);
        } catch (Exception e) {
            holder.progressView.setDonut_progress("0");
            holder.progressView.setText("0%");
        }
    }

    @Override
    public int getItemCount() {
        return seatRates.size();
    }

    static class SeatRateViewHolder extends RecyclerView.ViewHolder {
        TextView tvRoomName;
        DonutProgress progressView;

        public SeatRateViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRoomName = itemView.findViewById(R.id.tvRoomName);
            progressView = itemView.findViewById(R.id.progressView);
        }
    }
}
